package cn.fengliu.controller.adm;

import cn.fengliu.Dao.ClzDao;
import cn.fengliu.entity.Clz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller//创建对象，并且装配到 IOC容器
@RequestMapping("/clz")
public class ClzController {
    @Autowired
     ClzDao clzDao;
    public ClzController() {
        System.out.println("ClzController 对象被创建了");
     //当前页数
    }
    @RequestMapping("/show")
    public String findAll(Model model,Integer curpage){
        int total = clzDao.getTotal();//总行数
        System.out.println(total);
        if (curpage==null){
            curpage=1; }//当前页数
        int curpagemax=total/6;//总页数
        System.out.println(curpagemax);
        System.out.println(curpage);
          if (curpagemax%6!=0){
              curpagemax=curpagemax+1;
          }
        System.out.println(curpagemax);
        List<Clz> clzs = clzDao.findPage((curpage-1)*6,6);
        System.out.println("show clz"+clzs);
          model.addAttribute("curpage" ,curpage);
    model.addAttribute("curpagemax",curpagemax);
//        List<Map<String, Object>> clzs = clzDao.findAll();
        model.addAttribute("clzs",clzs);
        return "/adm/clz/clz";
    }
    @RequestMapping("/nextpage")
    public  String nextpage(Integer curpage,Model model){
    if (curpage==null)curpage=1;
        int total = clzDao.getTotal();
        if (curpage==-1){
            curpage=total/6;
        }
       if (curpage*6<total)curpage++;
        model.addAttribute("Clz",new Clz());
        return "forward:/clz/show?curpage="+curpage;
    }
@RequestMapping("/prevpage")
public  String prevpage(Integer curpage,Model model){
    if (curpage==null)curpage=1;
    if (curpage>1)curpage--;
    model.addAttribute("Clz",new Clz());
    return "forward:/clz/show?curpage="+curpage;
}


    @GetMapping("/go2add")
    public String go2add(Model model){

   model.addAttribute("action","add");
   Clz clz=new Clz();
   model.addAttribute("clz",clz);
     return "forward:/clz/show";
    }
    @RequestMapping("/add")
    public  String add (Clz clz, Model model){
        if (!clz.getClzno().equals("") && clz.getClzname().equals("")) {
            clzDao.add(clz);
        }
        model.addAttribute("info","缺少信息");
        return "forward:/clz/nextpage?curpage=-1";

    }
    @GetMapping("/go2update")
    public String go2update(Model model ,String clzno,Integer curpage){
        if (curpage==null)curpage=1;
        Clz clz = clzDao.findOne(clzno);
        model.addAttribute("action","update");
//        model.addAttribute("clz" ,clz);
        model.addAttribute("clzno",clz.getClzno());
        model.addAttribute("clzname",clz.getClzname());

        return "forward:/clz/show?curpage="+curpage;
    }

    @RequestMapping("/update")
    public  String update (Model model,Clz clz, Integer curpage){
        if (curpage==null)curpage=1;

        if (!clz.getClzname().equals("")&& clz.getClzno().equals("")) {
            clzDao.update(clz);
        }
        model.addAttribute("info","缺少信息");
        return "forward:/clz/show?curpage="+curpage;
    }
    @GetMapping("/del")
     public  String del(Model model,String clzno){
        clzDao.del(clzno);
        return  "forward:/clz/show";
    }

}
